package com.appleyk.config4;

import com.appleyk.annotation.EnableService;
import com.appleyk.service.BService;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;

/**
 * <p>越努力，越幸运</p>
 * 一般来说，selectImports还有另外一种导入实现，就是通过annotationMetadata的属性值
 * 来动态的导入所需的bean，我们可以在AnnotationMetadata所在的类上额外添加注解
 * 通过获取annotationMetadata对应注解的属性值，来动态的指定谁将会被导入到ioc容器中
 * @author appleyk
 * @version V.0.1.1
 * @blob https://blog.csdn.net/appleyk
 * @date created on  9:12 下午 2020/12/9
 */
public class ServiceSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        // 拿到配置类元数据上的@EnableService注解
        Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(EnableService.class.getName());
        String[] names = (String[])attributes.get("value");
        String[] imports  ;
        if(names.length>0){
            System.out.println("有属性值了，准备导入的属性值有："+names.length+"个");
            imports = new String[names.length];
            for (int i = 0; i <names.length ; i++) {
                if("B".equals(names[i])){
                    imports[i] = BService.class.getName();
                }
            }
            return imports;
        }
        return new String[0];
    }
}
